package com.example.netty.util;

import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.bytes.ByteArrayEncoder;

import java.util.Random;
import java.util.concurrent.TimeUnit;

public class HexClientOne {
    private static final String HOST = "47.119.186.103";
    private static final int PORT = 9999; // 修改为实际端口
    private static final String [] HEX_DATA =
            {
            "FAF502F800BE134C00000940000000000000092C028A019800000000000000000000020800F3011F009800B900380166014409D002CB0000000001E200F30748000202AA031200000543026F01600000012E02830288028C028E028602870299000008D7092C098B08F00940094F092F097C0000000000000000000000000000000002FD00BF136C0000098000000000000009C2029D01AC000000000000000000000203010B0111009C00B00044016B0150098F02D30000000001EF00FD0781000302AA031200000507027B0162000001340297029C02A002A2029A02A502990000099F0990098B0A1C0A0809B309F70A44000000000000000000000000000000000000014F0563000001E901A9000000FD00000000000000000F400F7A000000000000015D04F5000001C8018200000105000000000000000003B835D8000000000000014F0588000001E901A9000000FD0000000000000000000000000000000005DC00560138000003430300020400F703B80F40069613880000000000000000030C00000000047001F002AF02F41078019600FC000000000000000000000000000000000000000000010001000100010001000100010001000000000000000002AF0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000F33F",
            "FAF502D100B4124E000009400000000000000A3E02AB01D200000000000000000000021F0109010300AE00D0004A0154013D099702A50000000001E200F30748000202E203370000055C0291016D0000013C02A502A902AD02AF02A702A602BA00000A1509F90A5C0A1E0A700A7D0A5F0AAB00000000000000000000000000000000029100B4124E0000098000000000000009C802A201AC000000000000000000000221010C010600B000D1004C0157013F099902A70000000001EF00FD0781000302B003180000050D028001670000013A029C02A002A402A6029F02AA02A0000009A5099509910A220A0F09B909FD0A4B000000000000000000000000000000000000014F0562000001E201CF000000FF00000000000000000F400F7A0000000005DE013704F30000030F02ED020000FE04970F3E0849138A075435D8000000000000014F0587000001B20182000001030000000000000000000000000000000005DE002A005F000003140338020100F302BD0F3F04F113890000000000000000030C00000000047001F002AF02F41078019600FC000000000000000000000000000000000000000000010001000100010001000100010001000000000000000002AF0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000F33F",
            "FBF5020B011C000108940302000001B2011D0000071E028000010217011C000208C7031600010232011F00010938033E000100B7011D00010300010E000100A30123000402AC00F0000100DF011A000303A8014A0001010A011E0001045D01860001026D011A00020A30039800010285011D00010A9403C0000100390123000100EF005000010042012500010115005A0001442600000000000000000000000000000124012701260127016201780BE700010131013E013D0129016901820D180001011E01200123011E0159016F0E5E000101160113011500000143013702320001011601130117000000000137046E0001011A0120011900000000013D049800010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007B40CA1F000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000F33F",
            "FBF5020E011D000108A1030C000001B4011E00000727028000010219011E000208CF0320000102340120000109400348000100B9011F00010308010E000100A50124000402B400F0000100E1011B000303B0014A0001010C01200001046501860001026F011C00020A38039800010286011E00010A9903C00001003B0124000100F7005000010043012300010119005A0001447900000000000000000000000000000122012501240126016001760BE70001012F013C013B01270168017F0D180001011D011E0122011F0157016C0E5E000101130111011300000141013602320001011401110115000000000136046E00010118011C011B00000000013F0498000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000088B8D742000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000F33F"
            };
    public static void main(String[] args) throws Exception {
        EventLoopGroup group = new NioEventLoopGroup();
        try {
            Bootstrap b = new Bootstrap();
            b.group(group)
                    .channel(NioSocketChannel.class)
                    .handler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        protected void initChannel(SocketChannel ch) {
                            ChannelPipeline p = ch.pipeline();
                            p.addLast(new ByteArrayEncoder());
                            p.addLast(new HexClientHandler());
                        }
                    });

            // 连接服务器
            ChannelFuture f = b.connect(HOST, PORT).sync();
            f.channel().closeFuture().sync();
        } finally {
            group.shutdownGracefully();
        }
    }

    private static class HexClientHandler extends ChannelInboundHandlerAdapter {
        private ChannelHandlerContext ctx;

        @Override
        public void channelActive(ChannelHandlerContext ctx) {
            this.ctx = ctx;
            // 每秒发送一次数据
            ctx.executor().scheduleAtFixedRate(this::sendData, 0, 1, TimeUnit.SECONDS);
        }

        private void sendData() {
            if (ctx.channel().isActive()) {
                //将HEX字符串转换为字节数组
                Random r=new Random();
                int index= r.nextInt(HEX_DATA.length);
                byte[] hexBytes=hexStringToByteArray(HEX_DATA[index]);
                ByteBuf buf = Unpooled.wrappedBuffer(hexBytes);
                ctx.writeAndFlush(buf);
                System.out.println("Sent " + hexBytes.length + " bytes");
            }
        }

        @Override
        public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
            cause.printStackTrace();
            ctx.close();
        }

        private byte[] hexStringToByteArray(String s) {
            int len = s.length();
            byte[] data = new byte[len / 2];
            for (int i = 0; i < len; i += 2) {
                data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
                        + Character.digit(s.charAt(i+1), 16));
            }
            return data;
        }
    }
}
